package com.lmf.mock.starter.autoconfigure.controller;

import com.lmf.mock.starter.autoconfigure.entity.MockGroup;
import com.lmf.mock.starter.autoconfigure.service.MockGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Mock分组管理控制器
 */
@RestController
@RequestMapping("/mock/group")
public class MockGroupController {

    @Autowired
    private MockGroupService mockGroupService;

    /**
     * 获取所有分组
     */
    @GetMapping("/list")
    public ResponseEntity<List<MockGroup>> getAllGroups() {
        List<MockGroup> groups = mockGroupService.list();
        return ResponseEntity.ok(groups);
    }

    /**
     * 获取启用的分组
     */
    @GetMapping("/enabled")
    public ResponseEntity<List<MockGroup>> getEnabledGroups() {
        List<MockGroup> groups = mockGroupService.getEnabledGroups();
        return ResponseEntity.ok(groups);
    }

    /**
     * 根据ID获取分组
     */
    @GetMapping("/{id}")
    public ResponseEntity<MockGroup> getGroupById(@PathVariable Long id) {
        MockGroup group = mockGroupService.getGroupById(id);
        if (group != null) {
            return ResponseEntity.ok(group);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 创建分组
     */
    @PostMapping
    public ResponseEntity<Boolean> createGroup(@RequestBody MockGroup group) {
        boolean result = mockGroupService.createGroup(group);
        return ResponseEntity.ok(result);
    }

    /**
     * 更新分组
     */
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> updateGroup(@PathVariable Long id, @RequestBody MockGroup group) {
        group.setId(id);
        boolean result = mockGroupService.updateGroup(group);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除分组
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteGroup(@PathVariable Long id) {
        boolean result = mockGroupService.deleteGroup(id);
        return ResponseEntity.ok(result);
    }
}
